package weaver.jobs.cetc.duss.cron;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-02
 * @Description: E8：收文：公文类型为阅件：从流程发起之日起至未流程归档，时间超过30天（包含），
 * 以0.5为单位增加天数，此功能可在流程里面添加一个字段，每半天刷新一次，去更改超时天数，
 * 如果超时了，该字段需要放在待办列表后，以红色展现
 * 公文类型为办件时：会有一个上报日期，如果当前日期还没有到上报日期，
 * 那待办列表后会显示：剩余天数：XX天，如果超过上报天数显示：已超期：XX天；
 * 0.5天为一个单位，每半天刷新一次
 * @ModifyLog:
 **/

public class WeaSwlccqCronJob extends BaseCronJob {
    @Override
    public void execute() {
        String fullDate = DateUtil.getFullDate();
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        String formid = "129";
        baseBean.writeLog( ">>>>WeaSwlccqCronJob>>>>>fullDate="+fullDate );
        //公文类型为阅件,获取超时30天的数据
        rs.execute("select a.requestid,a.sfrq,(datediff(day,a.sfrq,getdate())-30) as csts from formtable_main_"+formid+" a " +
                "join workflow_requestbase b on a.requestid=b.requestid" +
                " where b.currentnodetype<3 and lx=0 and datediff(day,a.sfrq,getdate())>=30");
        int csts_counts = rs.getCounts();
        int csts_count = 0;
        while (rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            String csts = Util.null2String(rs.getString("csts"));
            String sql ="update formtable_main_"+formid+" set csts="+csts+" where requestid="+requestid;
            baseBean.writeLog( ">>>>WeaSwlccqCronJob>>>>csts>>>>>sql="+sql );
            boolean isUpdate = rs.execute(sql);
            if (isUpdate) {
                csts_count++;
            }
        }
        baseBean.writeLog( ">>>>WeaSwlccqCronJob>>>>>banjian>>>>csts_counts="+csts_counts+",csts_count="+csts_count );
        //公文类型为办件，获取剩余天数数据
        rs.execute("select a.requestid,a.sbrq,datediff(day,getdate(),a.sbrq) as sytshj,a.syts from formtable_main_"+formid+" a " +
                "join workflow_requestbase b on a.requestid=b.requestid" +
                " where b.currentnodetype<3 and lx=1 and a.sbrq>getdate()");
        while (rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            String sytshj = Util.null2String(rs.getString("sytshj"));
            String syts = Util.null2String(rs.getString("syts"));
            String sql="";
            if("".equals(syts)){
                sql ="update formtable_main_"+formid+" set syts="+sytshj+" where requestid="+requestid;
            }else {
                sql ="update formtable_main_"+formid+" set syts=cast(syts as decimal(18,1))-0.5 where requestid="+requestid;
            }
            boolean isUpdate = rs.execute(sql);
            baseBean.writeLog( ">>>>WeaSwlccqCronJob>>>>>status="+isUpdate+">>>>>sql="+sql );
        }

        //公文类型为办件，获取已超期天数数据
        rs.execute("select a.requestid,a.sbrq,datediff(day,a.sbrq,getdate()) as ycqtshj,a.ycqts from formtable_main_"+formid+" a " +
                "join workflow_requestbase b on a.requestid=b.requestid" +
                " where b.currentnodetype<3 and lx=1 and a.sbrq<=getdate()");
        while (rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            String ycqts = Util.null2String(rs.getString("ycqts"));
            String ycqtshj = Util.null2String(rs.getString("ycqtshj"));
            String sql="";
            if("".equals(ycqts)){
                 sql ="update formtable_main_"+formid+" set ycqts="+ycqtshj+" where requestid="+requestid;
            }else {
                 sql ="update formtable_main_"+formid+" set ycqts=cast(ycqts as decimal(18,1))+0.5 where requestid="+requestid;
            }
            boolean isUpdate = rs.execute(sql);
            baseBean.writeLog( ">>>>WeaSwlccqCronJob>>>>>status="+isUpdate+">>>>>sql="+sql );
        }
    }

}
